<template>
  <div class="login">
    <van-form @submit="onSubmit">
      <van-field
        v-model="userName"
        name="userName"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { reqLogin } from "../../api/user";
import { Toast } from "vant";
import { setToken } from "../../util/auth";
export default {
  data() {
    return {
      userName: "",
      password: "",
      imgurl: "",
    };
  },
  computed: {},
  watch: {},

  methods: {
    // 提交方法
    async onSubmit(values) {
      console.log("submit", values);
      const result = await reqLogin({
        userName: this.userName,
        password: this.password,
      });
      console.log(1111, result);
      if (result.code === "success") {
        localStorage.setItem("token",result.token)
        setToken(result.token);// 存入cokie里面了一份儿
        //需要存入vuex中一份儿
        this.$store.commit('changetoken',result.token)
        this.$router.push("/home");
      } else {
        Toast(result.message);
      }
    },
  },
  created() {
    console.log(111,this.$store)
    // console.log(666,this.result)
  },
  mounted() {},
  components: {},
};
</script>
<style scoped></style>
